package cn.stylefeng.guns.modular.dailyandrealtimeprices.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.dailyandrealtimeprices.service.QyuanPowerSdPricesService24;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.dailyandrealtimeprices.entity.QyuanPowerSdPrices;
import cn.stylefeng.guns.modular.dailyandrealtimeprices.enums.QyuanPowerSdPricesExceptionEnum;
import cn.stylefeng.guns.modular.dailyandrealtimeprices.mapper.QyuanPowerSdPricesMapper;
import cn.stylefeng.guns.modular.dailyandrealtimeprices.pojo.request.QyuanPowerSdPricesRequest;
import cn.stylefeng.guns.modular.dailyandrealtimeprices.service.QyuanPowerSdPricesService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Date;
import java.math.BigDecimal;

/**
 * 电价表，记录每天96个时间点的不同电价情况(日前和实时)业务实现层
 *
 * @author 我是谭先生🐻
 * @date 2025/03/17 11:36
 */
@Service
public class QyuanPowerSdPricesServiceImpl extends ServiceImpl<QyuanPowerSdPricesMapper, QyuanPowerSdPrices> implements QyuanPowerSdPricesService {


    @Override
    public void add(QyuanPowerSdPricesRequest qyuanPowerSdPricesRequest) {
        QyuanPowerSdPrices qyuanPowerSdPrices = new QyuanPowerSdPrices();
        BeanUtil.copyProperties(qyuanPowerSdPricesRequest, qyuanPowerSdPrices);
        this.save(qyuanPowerSdPrices);
    }

    @Override
    public void del(QyuanPowerSdPricesRequest qyuanPowerSdPricesRequest) {
        QyuanPowerSdPrices qyuanPowerSdPrices = this.queryQyuanPowerSdPrices(qyuanPowerSdPricesRequest);
        this.removeById(qyuanPowerSdPrices.getId());
    }

    @Override
    public void edit(QyuanPowerSdPricesRequest qyuanPowerSdPricesRequest) {
        QyuanPowerSdPrices qyuanPowerSdPrices = this.queryQyuanPowerSdPrices(qyuanPowerSdPricesRequest);
        BeanUtil.copyProperties(qyuanPowerSdPricesRequest, qyuanPowerSdPrices);
        this.updateById(qyuanPowerSdPrices);
    }

    @Override
    public QyuanPowerSdPrices detail(QyuanPowerSdPricesRequest qyuanPowerSdPricesRequest) {
        return this.queryQyuanPowerSdPrices(qyuanPowerSdPricesRequest);
    }

    @Override
    public PageResult<QyuanPowerSdPrices> findPage(QyuanPowerSdPricesRequest qyuanPowerSdPricesRequest) {
        LambdaQueryWrapper<QyuanPowerSdPrices> wrapper = createWrapper(qyuanPowerSdPricesRequest);
        Page<QyuanPowerSdPrices> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<QyuanPowerSdPrices> findList(QyuanPowerSdPricesRequest qyuanPowerSdPricesRequest) {
        LambdaQueryWrapper<QyuanPowerSdPrices> wrapper = this.createWrapper(qyuanPowerSdPricesRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 我是谭先生🐻
     * @date 2025/03/17 11:36
     */
    private QyuanPowerSdPrices queryQyuanPowerSdPrices(QyuanPowerSdPricesRequest qyuanPowerSdPricesRequest) {
        QyuanPowerSdPrices qyuanPowerSdPrices = this.getById(qyuanPowerSdPricesRequest.getId());
        if (ObjectUtil.isEmpty(qyuanPowerSdPrices)) {
            throw new ServiceException(QyuanPowerSdPricesExceptionEnum.QYUAN_POWER_SD_PRICES_NOT_EXISTED);
        }
        return qyuanPowerSdPrices;
    }

    /**
     * 创建查询wrapper
     *
     * @author 我是谭先生🐻
     * @date 2025/03/17 11:36
     */
    private LambdaQueryWrapper<QyuanPowerSdPrices> createWrapper(QyuanPowerSdPricesRequest qyuanPowerSdPricesRequest) {
        LambdaQueryWrapper<QyuanPowerSdPrices> queryWrapper = new LambdaQueryWrapper<>();

        Integer id = qyuanPowerSdPricesRequest.getId();
        String usageDate = qyuanPowerSdPricesRequest.getUsageDate();
        Integer timePoint = qyuanPowerSdPricesRequest.getTimePoint();
        String timeSlot = qyuanPowerSdPricesRequest.getTimeSlot();
        BigDecimal dayaheadPrice = qyuanPowerSdPricesRequest.getDayaheadPrice();
        BigDecimal realtimePrice = qyuanPowerSdPricesRequest.getRealtimePrice();
        String createdAt = qyuanPowerSdPricesRequest.getCreateTime();
        String updatedAt = qyuanPowerSdPricesRequest.getUpdateTime();

        queryWrapper.eq(ObjectUtil.isNotNull(id), QyuanPowerSdPrices::getId, id);
        queryWrapper.eq(ObjectUtil.isNotNull(usageDate), QyuanPowerSdPrices::getUsageDate, usageDate);
        queryWrapper.eq(ObjectUtil.isNotNull(timePoint), QyuanPowerSdPrices::getTimePoint, timePoint);
        queryWrapper.like(ObjectUtil.isNotEmpty(timeSlot), QyuanPowerSdPrices::getTimeSlot, timeSlot);
        queryWrapper.eq(ObjectUtil.isNotNull(dayaheadPrice), QyuanPowerSdPrices::getDayaheadPrice, dayaheadPrice);
        queryWrapper.eq(ObjectUtil.isNotNull(realtimePrice), QyuanPowerSdPrices::getRealtimePrice, realtimePrice);
        queryWrapper.eq(ObjectUtil.isNotNull(createdAt), QyuanPowerSdPrices::getCreateTime, createdAt);
        queryWrapper.eq(ObjectUtil.isNotNull(updatedAt), QyuanPowerSdPrices::getUpdateTime, updatedAt);

        return queryWrapper;
    }

}